package com.windy.kafka.sender.annotation.config;

import org.aopalliance.aop.Advice;
import org.springframework.aop.framework.autoproxy.AbstractBeanFactoryAwareAdvisingPostProcessor;
import org.springframework.beans.factory.BeanFactory;
import org.springframework.lang.Nullable;
import org.springframework.util.Assert;

import java.lang.annotation.Annotation;

/**
 * Created by liyufeng on 2022/3/22
 */
public class MessageAnnotationBeanPostProcessor extends AbstractBeanFactoryAwareAdvisingPostProcessor {
    @Nullable
    private Class<? extends Annotation> messageAnnotationType;

    public Advice advice;

    public void setAdvice(Advice advice) {
        this.advice = advice;
    }

    public void setAsyncAnnotationType(Class<? extends Annotation> messageAnnotationType) {
        Assert.notNull(messageAnnotationType, "'messageAnnotationType' must not be null");
        this.messageAnnotationType = messageAnnotationType;
    }

    /**
     * @param beanFactory
     */
    @Override
    public void setBeanFactory(BeanFactory beanFactory) {
        super.setBeanFactory(beanFactory);
        MessageAnnotationAdvisor advisor = new MessageAnnotationAdvisor(this.advice);
        if (this.messageAnnotationType != null) {
            advisor.setMessageAnnotationType(this.messageAnnotationType);
        }
        advisor.setBeanFactory(beanFactory);
        this.advisor = advisor;
    }

}
